package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JList;
import java.awt.Panel;
import java.awt.Button;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.ScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import java.awt.SystemColor;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import entity.UserLoginInfo;
import controllers.ClientController;
import controllers.DBConnection;
import controllers.WorkerController;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * this class open the main login window of all user
 *
 */

public class LoginGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel Login;
	private JTextField textFieldID;
	private JPasswordField passwordField;
	public	String text;
	public final static ImageIcon IMG_LOGIN_BACKGROUND = new ImageIcon("images/LoginP.jpg");
	static UserLoginInfo uLoginInfo;
	ClientController clientCont;
	private JTextField textFieldIP;
	private JTextField textFieldPort;
	static int port=0;
	static String IP=null;
	static String pass;
	static String ID;
	
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginGUI frame = new LoginGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the Login GUI frame.
	 */
	
	public LoginGUI() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				try{
				DBConnection conn= new DBConnection();
				  java.sql.PreparedStatement psl = conn.getDBConnection().prepareStatement("Update sgi_system.users SET status=? ");
				  psl.setInt(1, 0);
				  psl.executeUpdate();
				}catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		setTitle("Login");
		
		/*java.util.Calendar cal = Calendar.getInstance();
		java.util.Date utilDate = new java.util.Date(); // your util date
		cal.setTime(utilDate);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);    
		java.sql.Date sqlDate = new java.sql.Date(cal.getTime().getTime()); // your sql date
		System.out.println("utilDate:" + utilDate);
		System.out.println("sqlDate:" + sqlDate); */
		
	/*	SimpleDateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		java.util.Date date = new java.util.Date();	
		String s=dateTime.format(date);
		//System.out.println(s);
		java.sql.Time sqlDate1 = java.sql.Time.valueOf(s);
		System.out.println("sqlDate1:.. " + sqlDate1);
		java.sql.Timestamp sqlDate7 = java.sql.Timestamp.valueOf(s);
		System.out.println("sqlDate111:.. " + sqlDate7);*/
		
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		setBounds(100, 100, 629, 365);
		Login = new JPanel();
		Login.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(Login);
		Login.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setForeground(Color.BLUE);
		panel.setBackground(Color.LIGHT_GRAY);
		Login.add(panel);
		
		textFieldID = new JTextField();
		textFieldID.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//String text=textField.getText();
				
				
			}
		});
		textFieldID.addInputMethodListener(new InputMethodListener() {
			public void caretPositionChanged(InputMethodEvent arg0) {
			}
			public void inputMethodTextChanged(InputMethodEvent arg0) {
					
			}
		});
		
		
		textFieldID.setToolTipText("Enter ID");
		textFieldID.setBounds(97, 20, 120, 20);
		textFieldID.setColumns(10);
		
		JLabel lblUserid = new JLabel("UserID");
		lblUserid.setForeground(Color.BLUE);
		lblUserid.setBackground(Color.BLUE);
		lblUserid.setBounds(10, 23, 64, 14);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setForeground(Color.BLUE);
		lblPassword.setBackground(Color.BLUE);
		lblPassword.setBounds(10, 61, 64, 14);
		panel.setLayout(null);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {            /*login button action */
				try{
					btnLoginActionPerformed(e);
					
				}catch(IOException e1) {
					e1.printStackTrace();
				}
			
			}
			
		});
		
		
		
		btnLogin.setToolTipText("Confirm Login");
		btnLogin.setBounds(97, 226, 71, 23);
		panel.add(btnLogin);
		panel.add(lblUserid);
		panel.add(lblPassword);
		panel.add(textFieldID);
		
		passwordField = new JPasswordField();
		passwordField.setToolTipText("Enter Password");
		passwordField.setBounds(97, 57, 120, 23);
		panel.add(passwordField);
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {            /*Cancel button action */
				try{
					btnCancelActionPerformed(e);
					
				} catch(IOException e1) {
					e1.printStackTrace();
				}			
			}
			
		});

		btnCancel.setBounds(220, 226, 76, 23);
		panel.add(btnCancel);
		
		JCheckBox chckbxRemmemberMe = new JCheckBox("remmember me");
		chckbxRemmemberMe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		chckbxRemmemberMe.setBounds(139, 267, 120, 23);
		panel.add(chckbxRemmemberMe);
		
		JLabel lblIPAddress = new JLabel("IP Addrees");
		lblIPAddress.setBounds(10, 121, 64, 14);
		panel.add(lblIPAddress);
		
		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(10, 153, 46, 14);
		panel.add(lblPort);
		
		textFieldIP = new JTextField();
		textFieldIP.setBounds(97, 118, 120, 20);
		panel.add(textFieldIP);
		textFieldIP.setColumns(10);
		
		textFieldPort = new JTextField();
		textFieldPort.setBounds(96, 150, 121, 20);
		panel.add(textFieldPort);
		textFieldPort.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("images//LoginP.jpg"));
		lblNewLabel.setBounds(0, 0, 619, 317);
		panel.add(lblNewLabel);
		
		
		
	}
	
	/**
	 * add Popup Component
	 */
	
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	public void btnLoginActionPerformed(ActionEvent e) throws IOException  {
			
		
		  //get user id as inserted
		LoginGUI.ID=textFieldID.getText();
		
		
		LoginGUI.pass=passwordField.getText();   // get user password as inserted
		
		//check if all fields not empty
		if(ID.equals(""))
		{
			JOptionPane.showMessageDialog(null, "No ID is entered, Try again ");
		}
		else if (pass.equals("") ) 
		{
			JOptionPane.showMessageDialog(null, "No Password is entered, Try again");
		}
		else	if (    (!(ID.equals(""))) & (!( pass.equals("")))    )
		{
			             
		
	
		if(textFieldIP.getText().equals(""))
			LoginGUI.IP="127.0.0.1";    									// default ip address
		else
			LoginGUI.IP=textFieldIP.getText();
	
		if(textFieldPort.getText().equals(""))
			LoginGUI.port=5555;    									// default port number
		else
			LoginGUI.port=Integer.parseInt(textFieldPort.getText());	
		
		
		
		ArrayList<String> arrLogin=new ArrayList<String>();   //array use to keep info about the action ( login) and user info 
		arrLogin.add("Login");
		arrLogin.add(ID);
		arrLogin.add(pass);
		arrLogin.add(IP);		
		String po=String.valueOf(port);
		arrLogin.add(po);
		this.clientCont=new ClientController(IP, port);		
		clientCont.handleMessageFromGui(arrLogin);
		}
	}
	
	
	private void btnCancelActionPerformed(ActionEvent e) throws IOException{
		
		try{
			  DBConnection conn= new DBConnection();
			  java.sql.PreparedStatement psl = conn.getDBConnection().prepareStatement("Update sgi_system.users SET status=? ");	
			  psl.setInt(1, 0);
			  psl.executeUpdate();
			  dispose();
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
	
	
	public String getID(){
		return LoginGUI.ID;
	}
	
	public String getIP(){
		return LoginGUI.IP;
	}
	
	public String getPass(){
		return LoginGUI.pass;
	}
	
	public int getport(){
		return LoginGUI.port;
	}
}
